package com.jml.二期.树.二叉树.二叉搜索树;

/**
 * @author:jml
 * @date: 2019/11/25 14:46 @description：
 */
public class JMLBinarySearchTree {
	
	/**
	 * 节点元素值
	 */
    int data;

    /**
     * 左子树
     */
    JMLBinarySearchTree left;
    /**
     * 右子树
     */
    JMLBinarySearchTree right;
	
	public JMLBinarySearchTree(int data) {
		this.data = data;
	}
	
	/**
	 * 插入：左小右大
	 * @param root
	 * @param data
	 */
	public void insert(JMLBinarySearchTree root,int data){
        if(data>root.data){
	        //当根节点右子树为空的时候
            if(root.right==null){
                root.right = new JMLBinarySearchTree(data);
            }else{//递归
            	insert(root.right,data);
            }
        }else{
	        if(root.left==null){
		        root.left = new JMLBinarySearchTree(data);
	        }else{//递归
		        insert(root.left,data);
	        }
        }
    }
	
	/**
	 *
	 * @param root
	 */
	public void inSearch(JMLBinarySearchTree root) {
		if (root != null) {
			inSearch(root.left);
			System.out.println(root.data);
			inSearch(root.right);
		}
	}
	
	public static void main(String[] args) {
		JMLBinarySearchTree jmlBinarySearchTree = new JMLBinarySearchTree(10);
		jmlBinarySearchTree.insert(jmlBinarySearchTree,8);
		jmlBinarySearchTree.insert(jmlBinarySearchTree,11);
		jmlBinarySearchTree.insert(jmlBinarySearchTree,4);
		jmlBinarySearchTree.insert(jmlBinarySearchTree,9);
		jmlBinarySearchTree.insert(jmlBinarySearchTree,22);
		jmlBinarySearchTree.inSearch(jmlBinarySearchTree);
	}
    
}
